#!/bin/bash

ping -c2 -W2 www.baidu.com &> /dev/null
if [ $? -ne 0 ]; then
	echo "Network not Reachable..."
	exit 1
fi

# 加载functions文件
. /etc/init.d/functions

YUM=/usr/bin/yum
WGET=/usr/bin/wget
TAR=/bin/tar
TOOLS_DIR=/home/lavenliu/tools

# 设置日志文件及路径
LOG_FILE=/tmp/lnmp.log
if [ -f $LOG_FILE ] ; then
	> $LOG_FILE
fi

# following steps for Linux
## check nginx related
echo "Check Gnu/Linux Environment ..."
echo "To install Nginx, the linux box should pre-install:"
echo -e "\t1. openssl-devel"
echo -e "\t2. pcre-devel-devel"
echo -e "\t3. need nginx user or other"
echo "Now, beginning check the requirement"
rpm -q openssl-devel
if [ $? -eq 1 ] ; then
	echo "openssl-devel not installed"
	echo "install openssl-devel now..."
	$YUM install -y openssl-devel &> /dev/null
	if [ $? -eq 0 ]; then
		echo "openssl-devel installed successfully"
	else
		echo "openssl-devel installed failed"
	fi    
fi

rpm -q pcre-devel
if [ $? -eq 1 ] ; then
	echo "pcre-devel not installed"
	echo "install pcre-devel now..."
	$YUM install -y pcre-devel &> /dev/null
	if [ $? -eq 0 ]; then
		echo "pcre-devel installed successfully"
	else
		echo "pcre-devel installed failed"
	fi
fi

id nginx &> /dev/null
if [ $? -ne 0 ]; then
	echo "nginx user is not exists"
	echo "add nginx user now..."
	useradd nginx -s /sbin/nologin -M
fi

# following steps for Nginx
[ ! -d ${TOOLS_DIR} ] && mkdir -p /home/lavenliu/tools
cd /home/lavenliu/tools/
$WGET -q http://nginx.org/download/nginx-1.6.3.tar.gz
$TAR -xf nginx-1.6.3.tar.gz
cd nginx-1.6.3
./configure --user=nginx --group=nginx --prefix=/application/nginx-1.6.3 \
			--with-http_ssl_module \
			--with-http_stub_status_module &> /dev/null
if [ $? -ne 0 ] ; then
	echo "configure failed"
	exit 1
fi

make && make install
if [ $? -ne 0 ] ; then
	echo "make failed"
	exit 1
fi

ln -s /application/nginx-1.6.3 /application/nginx

# following steps for MySQL
useradd mysql -s /sbin/nologin -M
cd /home/lavenliu/tools && tar -xf mysql-5.5.32-linux2.6-x86_64.tar.gz
mv /home/lavenliu/tools/mysql-5.5.32-linux2.6-x86_64 /application/mysql-5.5.32
ln -s /application/mysql-5.5.32 /application/mysql
/application/mysql/scripts/mysql_install_db \
--basedir=/application/mysql \
--datadir=/application/mysql/data \
--user=mysql

chown -R mysql.mysql /application/mysql/

# following steps for PHP
yum install -y libpng-devel libjpeg-devel libtiff-devel libxml2-devel
yum install -y freetype-devel gd-devel curl-devel
yum install -y libmcrypt-devel mhash-devel mhash libxslt-devel libtool-ltdl-devel
cd ${TOOLS_DIR} && tar -xf php-5.3.27.tar.gz
cd php-5.3.27
./configure --prefix=/application/php-5.3.27 \
--with-mysql=/application/mysql \
--with-iconv-dir=/usr/local/libiconv \
--with-freetype-dir \
--with-jpeg-dir \
--with-png-dir \
--with-zlib \
--with-libxml-dir=/usr \
--enable-xml \
--disable-rpath \
--enable-safe-mode \
--enable-bcmath \
--enable-shmop \
--enable-sysvsem \
--enable-inline-optimization \
--with-curl \
--with-curlwrappers \
--enable-mbregex \
--enable-fpm \
--enable-mbstring \
--with-mcrypt \
--with-gd \
--enable-gd-native-ttf \
--with-openssl \
--with-mhash \
--enable-pcntl \
--enable-sockets \
--with-xmlrpc \
--enable-zip \
--enable-soap \
--enable-short-tags \
--enable-zend-multibyte \
--enable-static \
--with-xsl \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-ftp
make
make install
ln -s /application/php-5.3.27 /application/php
